﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace Chisel.Core
{
	/// <summary>
	/// A group of items in an entity.
	/// </summary>
	public class ItemGroup : EntityChild
	{
		public ItemGroup(string label)
			: base(label)
		{
			_mainPanel = new StackPanel() {
				Orientation = Orientation.Vertical
			};
			Content = _mainPanel;

			_labelPanel = new StackPanel() {
				Orientation = Orientation.Horizontal
			};
			_mainPanel.Children.Add(_labelPanel);

			LoadIcons();
			//_expandButton = new Button() {
			//    //Content = _icons["expanded"],
			//    Foreground = new SolidColorBrush(Colors.Transparent),
			//    BorderBrush = new SolidColorBrush(Colors.Transparent),
			//    BorderThickness = new Thickness(0),
			//    Height = 24,
			//    Width = 24,
			//    IsTabStop = false
			//};
			//_labelPanel.Children.Add(_expandButton);
			//_expandButton.Click += new RoutedEventHandler(OnExpandClick);

			_expandImage = new Image() {
				Margin = new Thickness(0)
			};
			_labelPanel.Children.Add(_expandImage);
			_expandImage.MouseLeftButtonDown += new MouseButtonEventHandler(OnExpandPressed);
			_expandImage.MouseEnter += new MouseEventHandler(OnExpandEnter);
			_expandImage.MouseLeave += new MouseEventHandler(OnExpandLeave);
			_expandImage.Source = _buttonImages["expanded"];
			IsExpanded = true;
			
			_labelPanel.Children.Add(_label);
			IsExpanded = true;
			_label.FontWeight = FontWeights.Bold;

			_itemPanel = new StackPanel() {
				Orientation = Orientation.Vertical
			};
			_mainPanel.Children.Add(_itemPanel);
		}

		private StackPanel _mainPanel;

		private StackPanel _labelPanel;

		public override Entity Entity
		{
			get { return base.Entity; }
			set
			{
				base.Entity = value;
				foreach (var item in Items.All)
					item.Entity = value;
			}
		}

		protected override void ResetStyle()
		{
			base.ResetStyle();

			_labelPanel.Background = ParentCanvas.StyleProvider.ItemGroupBrush;
		}

		#region The Button

		private Image _expandImage;

		/// <summary>
		/// Whether or not the child items are visible.
		/// </summary>
		public bool IsExpanded { get; private set; }

		/// <summary>
		/// Collapses the group so the children aren't visible.
		/// </summary>
		public void Collapse()
		{
			IsExpanded = false;
			_expandImage.Source = _buttonImages["collapsed-blue"];
			_itemPanel.Visibility = Visibility.Collapsed;
		}

		/// <summary>
		/// Expands the group so the children aren't visible.
		/// </summary>
		public void Expand()
		{
			IsExpanded = true;
			_expandImage.Source = _buttonImages["expanded-blue"];
			_itemPanel.Visibility = Visibility.Visible;
		}

		private Dictionary<string, ImageSource> _buttonImages = new Dictionary<string, ImageSource>();

		/// <summary>
		/// Loads the icons from the assembly.
		/// </summary>
		private void LoadIcons()
		{
			var asm = Assembly.GetExecutingAssembly();

			var names = new string[] { "collapsed", "expanded", "collapsed-blue", "expanded-blue" };
			foreach (var name in names)
			{
				var bitmap = new BitmapImage();
				bitmap.SetSource(asm.GetManifestResourceStream(String.Format("Chisel.Core.Resources.{0}.png", name)));
				_buttonImages[name] = bitmap;
			}
		}

		/// <summary>
		/// Handles the expand/collape button being clicked.
		/// </summary>
		private void OnExpandPressed(object sender, MouseButtonEventArgs e)
		{
			if (IsExpanded)
				Collapse();
			else
				Expand();
		}

		/// <summary>
		/// Change the expand button icon curing hover.
		/// </summary>
		private void OnExpandEnter(object sender, MouseEventArgs e)
		{
			if (IsExpanded)
				_expandImage.Source = _buttonImages["expanded-blue"];
			else
				_expandImage.Source = _buttonImages["collapsed-blue"];
		}

		/// <summary>
		/// Change the expand button icon during hover.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void OnExpandLeave(object sender, MouseEventArgs e)
		{
			if (IsExpanded)
				_expandImage.Source = _buttonImages["expanded"];
			else
				_expandImage.Source = _buttonImages["collapsed"];
		}

		#endregion


		#region Items

		private StackPanel _itemPanel;

		/// <summary>
		/// The group's items.
		/// </summary>
		public ActorCollection<Item> Items = new ActorCollection<Item>();

		/// <summary>
		/// Adds an item to the group.
		/// </summary>
		public void AddItem(Item item)
		{
			item.Entity = Entity;
			item.Group = this;
			Items.Add(item);
			_itemPanel.Children.Add(item);
		}

		/// <summary>
		/// Removes an item from the group.
		/// </summary>
		public void RemoveItem(Item item)
		{
			Items.Remove(item);
			_itemPanel.Children.Remove(item);
		}

		#endregion


		#region Selection

		protected override void SelectAction()
		{
			ParentCanvas.Actors.DeselectAll();
			Entity.Children.Select(this);
		}

		protected override void DeselectAllAction()
		{
			Entity.Children.DeselectAll();
		}

		#endregion
	}
}
